//
//  WBOneViewController.swift
//  Swift-WB
//
//  Created by 李西亚 on 2017/5/20.
//  Copyright © 2017年 lixiya. All rights reserved.
//

import UIKit
import SwiftyJSON
import HandyJSON

let kWBStatusCell_ID = "kWBStatusCell_ID"

class WBOneViewController: WBViewController {
    var pageIndex = 1
    lazy var dataList = [WBStatusViewModel]()    
    
    lazy var tableView: TableView = {
        var tab = TableView(frame: CGRect.zero, style: .plain)
        tab.delegate = self
        tab.dataSource = self
        tab.separatorColor = UIColor.clear
        tab.register(WBStatusCell.self, forCellReuseIdentifier: kWBStatusCell_ID)
        return tab
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print("---\(String(describing: WBUserDataHelper.shared.userAccount.access_token))---")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpUI()
        fetchStatusList()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func setUpUI() {
        view.addSubview(tableView)
        tableView.snp.makeConstraints({ (make) in
            make.edges.equalTo(view)
        })
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(loginAction))
    }
    
    func loginAction(bt: UIButton) {
        let vc = WebViewController(urlString: "https://api.weibo.com/oauth2/authorize?client_id=\(WBAppKey)&redirect_uri=\(WBRedirectURL)")
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
  
}

//MARK: - Requests
extension WBOneViewController {
    
    func fetchStatusList() {
        let parameters = ["access_token":"2.00PmFxxFLwpvKEb20d96670eNS7Y2E", "page":self.pageIndex] as [String : Any]
        WBDataHelper.shared.wbFetchStatusList(parameters: parameters, success: { (response) in
            if  let statuses = response["statuses"],
                let array =  JSONDeserializer<WBStatus>.deserializeModelArrayFrom(json: JSON(statuses).description) {
                for status in array {
                    let statusViewModel = WBStatusViewModel()
                    statusViewModel.status = status
                    self.dataList.append(statusViewModel)
                }
                self.tableView.reloadData()
            }

        }) { (error) in
        }
    }
    
}


//MARK: - UITableViewDelegate && UITableViewDataSource
extension WBOneViewController: UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataList.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.dataList[indexPath.row].cellHeight
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: kWBStatusCell_ID, for: indexPath) as! WBStatusCell
        
        cell.statusViewModel = self.dataList[indexPath.row]
        
        return cell
    }
    
}







